package com.triptogether.trip.adapters;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import com.triptogether.R;
import com.triptogether.shoppinglist.fragments.ShoppingListMainFragment;
import com.triptogether.trip.fragments.TripMainFragment;
import com.triptogether.utils.db.DBManager;
import com.triptogether.utils.db.models.Trip;

public class TripTabsPagerAdapter extends FragmentStatePagerAdapter {
	
	private Trip currentTrip;
	private Context context;

	public TripTabsPagerAdapter(Context context, FragmentManager fm, long tripId) {
	    super(fm);
	    this.context = context;
	    currentTrip = DBManager.getInstance(context).findTripById(tripId);
    }

	@Override
	public Fragment getItem(int i) {
		
		switch (i) {
		case 0:
			Bundle arguments = new Bundle();
			ShoppingListMainFragment fragment;
			fragment = new ShoppingListMainFragment();
			arguments.putLong(TripMainFragment.ARG_TRIP_ID, currentTrip.getId());
			fragment.setArguments(arguments);
			return fragment;
			
		default:
			return null;
		}
	}

	@Override
    public int getCount() {
	    return 1;
    }

	@Override
	public CharSequence getPageTitle(int position) {
		switch (position) {
		case 0:
			return context.getString(R.string.trip_main_fragment_shopping_list_tab_title, currentTrip.getTripName());
		default:
			return "";
		}
	}
	
	public void changeTrip(long newTripId) {
		currentTrip = DBManager.getInstance(context).findTripById(newTripId);
		notifyDataSetChanged();
	}
	
}